package com.exercises;

import java.util.HashMap;

/**
 * 9x9 的数独是否有效
 */
public class Exercise36 {
    public boolean isValidSudoku(char[][] board) {
        HashMap<Integer, Integer>[] rows = new HashMap[9];
        HashMap<Integer, Integer>[] columns = new HashMap[9];
        HashMap<Integer, Integer>[] boxes = new HashMap[9];
        for (int i = 0; i < 9; i++) {
            rows[i] = new HashMap<>();
            columns[i] = new HashMap<>();
            boxes[i] = new HashMap<>();
        }
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                HashMap<Integer, Integer> row = rows[i];
                HashMap<Integer, Integer> column = rows[j];
                HashMap<Integer, Integer> box = rows[i / 3 * 3 + j / 3];
                char value = board[i][j];
                if (value == '.') {
                    continue;
                }
                int key = (int) value;
                if (row.containsKey(key) || column.containsKey(key) || box.containsKey(key)) {
                    System.out.println(i);
                    System.out.println(j);
                    System.out.println(key);
                    System.out.println(row.containsKey(key));
                    System.out.println(column.containsKey(key));
                    System.out.println(box.containsKey(key));
                    return false;
                }
                row.put(key, 0);
                column.put(key, 0);
                box.put(key, 0);
            }
        }
        return true;
    }


}
